<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="10">
        <el-col :span="10">
          <el-input v-model="queryInfo.query" placeholder="请输入商品名称" clearable @clear="getGoodsList">
            <el-button slot="append" class="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="序号" width="width"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="width"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="width"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="width"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="width"> </el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-button type="primary" class="el-icon-edit" size="mini" @click="showEditDialog(scope.row)"></el-button>
            <el-button type="danger" class="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="this.queryInfo.pagenum" :page-sizes="[2, 5, 10, 15]" :page-size="this.queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="this.total"> </el-pagination>
    </el-card>

    
  </div>
</template>
<script>
export default {
  data() {
    return {
      goodsList: [],
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 20,
      },
      total: 0,
      //隐藏添加商品对话框
      addGoodsDialogVisible: false,
    };
  },
  created() {
    this.getGoodsList();
  },
  computed: {},
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      //   console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      }
      //提示获取商品列表成功
      this.$message.success("获取商品列表成功");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
      console.log(res.data.goods);

      this.goodsList.forEach((item) => {
        return (item.add_time = this.$tools.formatToTime(item.add_time));
      });
    },
    // 分页
    handleSizeChange(pagesize) {
      this.queryInfo.pagesize = pagesize;
      this.getGoodsList();
    },
    handleCurrentChange(pagenum) {
      this.queryInfo.pagenum = pagenum;
      this.getGoodsList();
    },
    // 跳转添加商品页面
    goAddPage(){
        this.$router.push('/goods/addGoods')
    },
    //监听添加商品对话框关闭
    addGoodsDialogClosed() {},
    //显示编辑对话框
    showEditDialog() {},
  },
};
</script>
<style lang="less" scoped>
</style>
